//
//  YKPopupView.m
//  YunKang
//
//  Created by leoking870 on 2017/10/3.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKPopupView.h"
#import "UIView+DimView.h"

@interface YKPopupView ()
@property (nonatomic, strong)UIViewController *contentViewController;
@end


@implementation YKPopupView

+ (void)dismissViewController:(UIViewController *)viewController {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    for (UIView* subview in window.subviews) {
        if ([subview isMemberOfClass:[YKPopupView class]]) {
            YKPopupView *view = (YKPopupView *)subview;
            if (view.contentViewController == viewController) {
                [view dismiss];
                break;
            }
        }
    }
}

- (void)dismiss {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIButton * closeButton = [window viewWithTag:55555];
    [UIView animateWithDuration:.3f animations:^{
        self.hidden = YES;
        [[[UIApplication sharedApplication]keyWindow]setDimViewAlpha:.0 forView:self];
        closeButton.hidden = YES;
    } completion:^(BOOL finished) {
        
        [[[UIApplication sharedApplication]keyWindow]removeDimViewForView:self];
        [self removeFromSuperview];
        [closeButton removeFromSuperview];
    }];
}

+ (instancetype)showPopupViewWithViewController:(UIViewController *)viewController {
    return [self showPopupViewWithViewController:viewController height:250];
}

+ (instancetype)showPopupViewWithViewController:(UIViewController *)viewController height:(CGFloat)height{
    YKPopupView *view = [[YKPopupView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = 10;
    view.contentViewController = viewController;
    [view addSubview:viewController.view];
    viewController.view.layer.cornerRadius = 5;
    [viewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    UIViewController* rootViewController = window.rootViewController;
//    [rootViewController addChildViewController:viewController];
//    [viewController didMoveToParentViewController:rootViewController];
    [window addViewAsDimBackgroundForView:view];
    [window addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(window);
        make.leading.equalTo(window.mas_leading).offset(15);
        make.height.mas_equalTo(height);
    }];
    
    UIButton *closeButton = [[UIButton alloc]init];
    [closeButton setImage:[UIImage imageNamed:@"yk_pop_close_icon"] forState:UIControlStateNormal];
    [window addSubview:closeButton];
    closeButton.tag = 55555;
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(view.mas_trailing).offset(-10);
        make.bottom.equalTo(view.mas_top);
    }];
    [closeButton addTarget:view action:@selector(onCloseButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [window layoutIfNeeded];
    
    [UIView animateWithDuration:.3f animations:^{
        [window setDimViewAlpha:.5f forView:view];
    }];
    
    
    return view;
}



- (void)onCloseButtonTapped:(id)sender {
    [self dismiss];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
